package cn.wolfcode.query;

import lombok.Getter;
import lombok.Setter;

import java.util.Collections;
import java.util.List;

@Getter
@Setter
public class PageResult<T> {
    //两传
    private int currentPage;
    private int pageSize;
    //两查
    private int totalCount;
    private List<T> list;
    //三计算
    private int totalPage;
    private int prevPage;
    private int nextPage;

    public PageResult(int currentPage, int pageSize, int totalCount, List<T> list) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.list = list;
        //要判断总条数是否小于等于每页显示条数(这样表示只有一页,总条数为0也只有一页)
        if (totalCount <= pageSize) {
            this.totalPage = 1;
            this.prevPage = 1;
            this.nextPage = 1;
            return;
        }
        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize
                : totalCount / pageSize + 1;
        this.prevPage = currentPage > 1 ? currentPage - 1 : 1;
        this.nextPage = currentPage < totalPage ? currentPage + 1 : totalPage;
    }

    //当totalCount为0时在提供一个封装空集合数据的构造器
    public PageResult(int currentPage, int pageSize) {
        this(currentPage, pageSize, 0, Collections.EMPTY_LIST);
    }
}
